package com.bwie.bwedu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.activity.MainActivity;
import com.bwie.bwedu.bean.Student;
import com.bwie.bwedu.ui.SnapUpCountDownTimerView;
import com.bwie.bwedu.ui.TimeTextView;
import com.bwie.bwedu.ui.dialog.BounceTopEnter;
import com.bwie.bwedu.ui.dialog.NormalDialog;
import com.bwie.bwedu.ui.dialog.OnBtnClickL;
import com.bwie.bwedu.ui.dialog.SlideBottomExit;
import com.bwie.bwedu.ui.dialog.base.BaseAnimatorSet;
import com.bwie.bwedu.utils.LogUtils;
import com.bwie.bwedu.utils.PhoneUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by zhang on 2016/7/22.
 */
public class WaittingConfirmFragmentAdapter extends MyBaseAdapter<Student>{

    private List<TimeTextView> tvs;
    private BaseAnimatorSet bas_in;
    private BaseAnimatorSet bas_out;
    private ViewHolder viewHolder;
    ItemClickListener itemClickListener;
    public  void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
//定义一个接口
public interface  ItemClickListener{
       void onInvalidClick(int position);
        void onValidClick(int position);
    }
    public WaittingConfirmFragmentAdapter(Context context, List<Student> list) {
        super(context, list);
        tvs=new ArrayList<>();
        bas_in = new BounceTopEnter();
        bas_out = new SlideBottomExit();
    }
    @Override
    public View myView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = View.inflate(getContext(), R.layout.fragment_waittingconfirm_item,null);
            viewHolder = new ViewHolder();
            viewHolder.imageView=(ImageView) convertView.findViewById(R.id.item_imageview);
            viewHolder.rushBuyCountDownTimerView = (SnapUpCountDownTimerView) convertView.findViewById(R.id.RushBuyCountDownTimerView);
            viewHolder.item_lianxi_name=(TextView) convertView.findViewById(R.id.item_lianxi_name);
            viewHolder.item_lianxi_phone=(TextView) convertView.findViewById(R.id.item_lianxi_phone);
            viewHolder.item_yuan_name=(TextView) convertView.findViewById(R.id.item_yuan_name);
            viewHolder.item_yuan_phone=(TextView) convertView.findViewById(R.id.item_yuan_phone);
            viewHolder.item_location=(TextView) convertView.findViewById(R.id.item_location);
            viewHolder.item_identification=(TextView) convertView.findViewById(R.id.item_identification);
            viewHolder.itme_button_no=(Button)convertView.findViewById(R.id.itme_button_no);
            viewHolder.item_button_yes=(Button)convertView.findViewById(R.id.item_button_yes);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        //设置监听---------------
        setListener(position,convertView);
        LogUtils.e("==========="+position);
        viewHolder.item_lianxi_name.setText("联系人:"+getList().get(position).gxxm);
        viewHolder.item_lianxi_phone.setText(getList().get(position).gxdh);
        viewHolder.item_yuan_phone.setText(getList().get(position).grsj);
        viewHolder.item_identification.setText("身份证号:"+getList().get(position).sfzh);
//        if((getList().get(position).xb)==1){
//
//        }else{
//
//        }
        viewHolder.item_yuan_name.setText("准学员姓名:"+getList().get(position).xm);
        //时间格式转换,实现倒计时功能
        String time = getStandardTime(Long.parseLong(getList().get(position).expireTime));

        String hour_time = time.substring(0, 2);

        int h_time = Integer.parseInt(hour_time);

        String minute_time = time.substring(4, 5);
        int m_time = Integer.parseInt(minute_time);

        String second_time = time.substring(6, 8);

        int s_time = Integer.parseInt(second_time);

        viewHolder.rushBuyCountDownTimerView.setTime(h_time, m_time, s_time);
        viewHolder.rushBuyCountDownTimerView.start();
        return convertView;
    }
    public class  ViewHolder{
         public ImageView imageView;
//         private SnapUpCountDownTimerView rushBuyCountDownTimerView;
        private TextView item_lianxi_name;
        private TextView item_lianxi_phone;
        private TextView item_yuan_name;
        private TextView item_yuan_phone;
        private TextView item_location;
        private TextView item_identification;
        private Button itme_button_no;
        SnapUpCountDownTimerView rushBuyCountDownTimerView;
        private Button item_button_yes;
    }
    public void setListener(final int position,final View convertView){
        //无效反馈按钮
        viewHolder.itme_button_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建对话框
                //对取消按钮设置监听
                //对确定按钮设置监听，网络请求
                final NormalDialog dialog = new NormalDialog(getContext());
                dialog.content("是否将该条信息返回市场部")//
                        .style(NormalDialog.STYLE_TWO)//
                        .titleTextSize(24)//
                        .showAnim(bas_in)//
                        .dismissAnim(bas_out)//
                        .show();

                dialog.setOnBtnClickL(
                        new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.dismiss();
                            }
                        },
                        new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                itemClickListener.onInvalidClick(position);
                                dialog.dismiss();
                            }
                        });
            }
        });
        //确认有效按钮监听
        viewHolder.item_button_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//创建对话框
                //对取消按钮设置监听
                //对确定按钮设置监听，网络请求
                final NormalDialog dialog = new NormalDialog(getContext());
                dialog.content("是否将该条信息返回市场部")//
                        .style(NormalDialog.STYLE_TWO)//
                        .titleTextSize(24)//
                        .showAnim(bas_in)//
                        .dismissAnim(bas_out)//
                        .show();

                dialog.setOnBtnClickL(
                        new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.dismiss();
                            }
                        },
                        new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                itemClickListener.onValidClick(position);
                                dialog.dismiss();
                            }
                        });
            }
        });
        //为联系人手机号设置监听
        viewHolder.item_lianxi_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhoneUtil.phone(getList().get(position).gxdh,(MainActivity)getContext(),convertView);
            }
        });
        //为学员手机号设置监听
        viewHolder.item_yuan_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhoneUtil.phone(getList().get(position).grsj,(MainActivity)getContext(),convertView);
            }
        });
    }
    //改变时间的方法
    public String getStandardTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        Date date = new Date(timestamp * 1000);
        sdf.format(date);
        return sdf.format(date);
    }

    public void ThreadState(){
        for (TimeTextView view:tvs){
            view.stopRun();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        if(viewHolder.item_textview_time.isRun()){
//            viewHolder.item_textview_time.stopRun();
//        }
    }
}
